package ch.ti.bfh.app.loodel.web.controller;

import ch.ti.bfh.app.loodel.service.TimeSlotData;
import ch.ti.bfh.app.loodel.service.timeslotschedule.TimeSlotScheduleData;
import ch.ti.bfh.app.loodel.service.vote.CurrentMemberTimeSlotVoteData;
import ch.ti.bfh.app.loodel.web.model.LoodelScheduleEvent;
import org.primefaces.model.DefaultScheduleEvent;

public class LoodelScheduleEventFactory {

    public static LoodelScheduleEvent createNewScheduleEvent(CurrentMemberTimeSlotVoteData currentMemberTimeSlotVoteData, boolean clickable) {
        DefaultScheduleEvent defaultScheduleEvent = new DefaultScheduleEvent();
        defaultScheduleEvent.setData(currentMemberTimeSlotVoteData);
        initGeneral(defaultScheduleEvent, currentMemberTimeSlotVoteData.getTimeSlotData());
        StringBuilder titleBuilder = buildTimeSlotDataTitle(currentMemberTimeSlotVoteData.getTimeSlotData());
        defaultScheduleEvent.setTitle(titleBuilder.toString());
        return new LoodelScheduleEvent(defaultScheduleEvent, false, currentMemberTimeSlotVoteData.isCurrentMemberVoted(), null, clickable);
    }

    public static LoodelScheduleEvent createNewScheduleEventWithExistingId(CurrentMemberTimeSlotVoteData currentMemberTimeSlotVoteData, String existingScheduleEventId, boolean clickable) {
        final LoodelScheduleEvent newLoodelScheduleEvent = createNewScheduleEvent(currentMemberTimeSlotVoteData, clickable);
        newLoodelScheduleEvent.setId(existingScheduleEventId);
        return newLoodelScheduleEvent;
    }

    public static LoodelScheduleEvent createNewScheduleEvent(TimeSlotScheduleData timeSlotScheduleData) {
        DefaultScheduleEvent defaultScheduleEvent = new DefaultScheduleEvent();
        defaultScheduleEvent.setData(timeSlotScheduleData);
        final TimeSlotData timeSlotData = timeSlotScheduleData.getTimeSlotData();
        initGeneral(defaultScheduleEvent, timeSlotData);
        StringBuilder titleBuilder = buildTimeSlotDataTitle(timeSlotData);
        if (timeSlotScheduleData.isAccepted() == null) {
            titleBuilder.append("Status: Open");
        } else if (timeSlotScheduleData.isAccepted() == true) {
            titleBuilder.append("Status: Accepted");
        } else {
            titleBuilder.append("Status: Rejected");
        }
        titleBuilder.append("\n");
        if (timeSlotScheduleData.isCurrentMemberVoted()) {
            titleBuilder.append("Your Vote: Yes");
        } else {
            titleBuilder.append("Your Vote: No");
        }
        defaultScheduleEvent.setTitle(titleBuilder.toString());
        return new LoodelScheduleEvent(defaultScheduleEvent, false, timeSlotScheduleData.isCurrentMemberVoted(), timeSlotScheduleData.isAccepted(), true);
    }

    private static StringBuilder buildTimeSlotDataTitle(TimeSlotData timeSlotData) {
        StringBuilder titleBuilder = new StringBuilder();
        titleBuilder.append("Module: " + timeSlotData.getLearningEventModuleName());
        titleBuilder.append("\n");
        titleBuilder.append("Location: " + timeSlotData.getLocationName());
        titleBuilder.append("\n");
        titleBuilder.append("Duration: " + timeSlotData.getDuration());
        titleBuilder.append("\n");
        titleBuilder.append("Votes: " + timeSlotData.getVoteCount());
        titleBuilder.append("\n");
        return titleBuilder;
    }

    private static void initGeneral(DefaultScheduleEvent defaultScheduleEvent, TimeSlotData timeSlotData) {
        defaultScheduleEvent.setStartDate(timeSlotData.getDateTimeInterval().getStart().toDate());
        defaultScheduleEvent.setEndDate(timeSlotData.getDateTimeInterval().getEnd().toDate());
        defaultScheduleEvent.setEditable(false);
    }


}
